package com.yvtian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yvtian.admin.dao.SysLogDao;
import com.yvtian.admin.pojo.SysLog;
import com.yvtian.admin.service.SysLogService;
import com.yvtian.admin.vo.LogVO;
import com.yvtian.constants.LogConstant;
import com.yvtian.utils.DateUtils;
import com.yvtian.utils.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SysLogServiceImpl extends ServiceImpl<SysLogDao,SysLog> implements SysLogService {

    @Resource
    private SysLogDao sysLogDao;

    @Override
    public IPage findByPage(Page page, SysLog sysLog) {
        QueryWrapper<SysLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(sysLog.getOperType() != null, "oper_type", sysLog.getOperType());
        queryWrapper.eq(sysLog.getStatus() != null, "status", sysLog.getStatus());
        queryWrapper.like(!StringHelper.isEmpty(sysLog.getUsername()), "username", sysLog.getUsername());
        //即使第一个参数是false，第三个也会执行，所以要注意空指针
        if (!StringHelper.isEmpty(sysLog.getSort())) {
            queryWrapper.orderBy(true, sysLog.getSort().equals("ascending"), "oper_time");
        }
        if (!StringHelper.isEmpty(sysLog.getOperTimes())) {
            String[] createDates = sysLog.getOperTimes().split("~");
            Date beginCreateDate = DateUtils.parse(DateUtils.FormatType.type1, createDates[0]);
            Date endCreateDate = DateUtils.parse(DateUtils.FormatType.type1, createDates[1]);
            queryWrapper.between("oper_time", beginCreateDate, endCreateDate);
        }
        return this.page(page,queryWrapper);
    }

    @Override
    public void saveLog(SysLog SysLog) {
        sysLogDao.insert(SysLog);
    }

    @Override
    public void del(ArrayList<Integer> ids) {
        sysLogDao.deleteBatchIds(ids);
    }

    @Override
    public void clear() {
        sysLogDao.delete(null);
    }

    @Override
    public List<List<String>> export() {
        List<SysLog> SysLogs = sysLogDao.selectList(null);
        return getExcelData(SysLogs);
    }

    private List<List<String>> getExcelData(List<SysLog> sysLogs) {
        List<List<String>> lists = new ArrayList<>();
        for (SysLog sysLog : sysLogs) {
            List<String> list = new ArrayList<>();
            list.add(String.valueOf(sysLog.getId()));
            list.add(sysLog.getUserId());
            list.add(sysLog.getUsername());
            list.add(LogConstant.getOerTypeStr(sysLog.getOperType()));
            list.add(sysLog.getContent());
            list.add(String.valueOf(sysLog.getCostTime()));
            list.add(sysLog.getMethod());
            list.add(sysLog.getRequestType());
            list.add(sysLog.getUrl());
            list.add(LogConstant.getOperStatus(sysLog.getStatus()));
            list.add(sysLog.getErrorMsg());
            list.add(sysLog.getResult());
            list.add(DateUtils.format(DateUtils.FormatType.type2, sysLog.getOperTime()));
            lists.add(list);
        }
        return lists;
    }


}
